package com.fau.mm.aop;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.fau.mm.dao.IBaseDao;
import com.fau.mm.entity.MedicalMediaInfo;
import com.fau.mm.entity.MedicalMediaInfoCosts;
import com.fau.mm.entity.MedicalMediaPlacement;
import com.fau.mm.entity.MedicalMedicine;

/**
 * @desc 媒体进线成本
 * @author dangjh
 * @datetime 2014年12月14日 上午10:41:31
 */
@Aspect
@Component
public class MediaCostsAop {

	@Resource
	private IBaseDao iBaseDao;
	
	@Pointcut("execution(* com.fau.mm.service.impl.MediaManagerService.savePlace(..))")
	private void savePlace() {}
	
	/**
	 * @desc 保存媒体进线成本信息
	 * @param joinPoint
	 * @author dangjh
	 * @datetime 2014年12月14日 上午11:21:45
	 */
	@AfterReturning("savePlace()")
	public void savePlace(JoinPoint joinPoint) {
		Object[] obj = joinPoint.getArgs();
		MedicalMediaPlacement place = (MedicalMediaPlacement) obj[0];
		MedicalMediaInfoCosts info = new MedicalMediaInfoCosts();
		MedicalMediaInfo mediaInfo = (MedicalMediaInfo) this.iBaseDao.getObjectByHql("from MedicalMediaInfo m where m.id = ? ", place.getMediaId());
		info.setMediaId(mediaInfo.getId());
		info.setMediaName(mediaInfo.getName());
		info.setCosts(place.getCosts());
		info.setPublishNumbers(place.getPublishNumbers());
		MedicalMedicine medicine = (MedicalMedicine) this.iBaseDao.getObjectByHql("from MedicalMedicine m where m.id = ? ", place.getMedicineId());
		info.setMedicineId(medicine.getId());
		info.setMedicineName(medicine.getName());
		info.setPutTime(place.getPlaceStartTime());
		info.setDescription(place.getDescription());
		info.setEid(mediaInfo.getEid());
		
		// 初始化统计数据信息
		info.setInNum(0);
		BigDecimal cost = new BigDecimal(0);
		info.setDailyCost(cost);
		info.setOrderNum(0);
		info.setOrderCost(cost);
		info.setEffectiveCost(cost);
		this.iBaseDao.saveObject(info);
	}
	
}
